package com.market.service.impl;

import com.market.common.exception.ServiceException;
import com.market.common.vo.PurchaseVo;
import com.market.common.vo.ResponseCode;
import com.market.entity.Department;
import com.market.entity.Purchase;
import com.market.entity.Role;
import com.market.entity.Worker;
import com.market.mapper.DepartmentMapper;
import com.market.mapper.WorkerMapper;
import com.market.service.DepartmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author :  KanShan
 * @version : 1.8
 * @Date: 2021/12/01/15:26
 * @注释 :
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    DepartmentMapper departmentMapper;

    @Resource
    WorkerMapper workerMapper;





    @Override
    public int insertDepartment(Department department) {
        int count = 0;
        List<Department> selectList = departmentMapper.selectList();
        for (Department department1 : selectList) {
            if (department1.getDepartmentName() != department.getDepartmentName()) {
                int insert = departmentMapper.insert(department);
                if (insert > 0) {
                    count = insert;
                    throw new ServiceException(ResponseCode.SUCCESS);
                } else {
                    throw new ServiceException(ResponseCode.ERROR);
                }
            } else {
                throw new ServiceException(ResponseCode.ERROR);
            }
        }
        return count;
    }


    @Override
    public int deleteDepartment(Long id) {
        try {
            return departmentMapper.delete(id);
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.ERROR);
        }

    }

    @Override
    public int updateDepartment(Department department) {
        int update = departmentMapper.update(department);
        if (update > 0) {
            return update;
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }

    }


    @Override
    public List<Department> findDepartment() {
        List<Department> departments = departmentMapper.selectList();
        return departments;
    }

    @Override
    public List<Department> findDepartmentByIdOrName(String string) {
        List<Department> departments = departmentMapper.selectListByIdOrName(string);

        return departments;
    }

    @Override
    public List<Department> find() {
        List<Department> selects = departmentMapper.selects();

        return selects;
    }

}